import PaBaseModel from '../../core/model/PaBaseModel';
import WebUploader from 'webuploader/dist/webuploader.js';

function equipmentInfoNewCtrl($scope, $rootScope, $http, ngDialog, fileUploaderService, paFetchService) {
  'ngInject';

  // 变量_this代表controllerAs 用于fileUploaderService
  let _this = this;

  // 表单数据
  this.editableEquipmentInfo = {
    id: '', // 主键
    deviceName: '', // 设备名称
    innerNo: '', // 档案编号
    usingSites: '', // 使用地点
    deviceModel: '', // 设备型号
    registrationNo: '', // 注册号
    license: '', // 拍照
    deviceLevel: '', // 设备等级
    operator: '', // 操作人员
    approachTime: '', // 进场时间
    exitTime: '', // 退场时间
    maintainSituation: '', // 维保情况
    isRented: '', // 是否租赁 0 否: 1 是
    tender: '', // 标段
    accessoryFileBeans: [], // 上传附件
  };


  this.editableEquipmentInfo = new PaBaseModel(this.editableEquipmentInfo);

  // 上传图片
  // 保存上传文件返回token
  this.avatarUploadToken = {}; // 头像图片token

  // 根据施工队获得人员列表 added by sunqy
  this.showWorkers = (team) => {
    if (team.initFlag) {
      team.isShowWorker = !!!team.isShowWorker;
      return;
    }
    $http
      .get(`${SERVER_CONTEXT}/api/construction/select/${team.teamId}`)
      .then((resp) => {
        team.initFlag = true;
        team.workerInfoBeanList = resp.data;
        team.isShowWorker = !!!team.isShowWorker;
        team.workerInfoBeanList.forEach((worker) => {
          worker.constructionTeamName = team.teamName;
          worker.constructionTeamId = team.teamId;
        })
      })
  }

  this.checkAll = function(selectTeam,$event){
    if (!selectTeam.workerInfoBeanList) {
      $http
        .get(`${SERVER_CONTEXT}/api/construction/select/${selectTeam.teamId}`)
        .then((resp) => {
          selectTeam.initFlag = true;
          selectTeam.workerInfoBeanList = resp.data;
          selectTeam.isShowWorker = !!!selectTeam.isShowWorker;
          selectTeam.workerInfoBeanList.forEach((worker) => {
            worker.constructionTeamName = selectTeam.teamName;
            worker.constructionTeamId = selectTeam.teamId;
            worker.checked = true;
          })
        })
    }
    var ele = $event.target;
    var flag = $(ele)[0].checked;
    if (flag) {
      selectTeam.isShowWorker = true;
    }
    $.each(this.teamWithOrgList,function(index,value){
      $.each(value.teamList,function(index1,team){
        if(team == selectTeam){
          $.each(team.workerInfoBeanList,function(index2,worker){
            worker.checked = flag;
          });
        }
      });
    });
  };

  // 判断全选人
  this.isChange = (id) => {
    var bool = true;
    this.teamWithOrgList.forEach((org) => {
      org.teamList.forEach((team) => {
        if (id == team.teamId) {
          team.workerInfoBeanList.forEach((item) => {
            if(!item.checked){
                bool = false;
            }
          })
          team.checked = bool;
        }
      })
    })
  }

  // 保存设备新增
  this.save = (param) => {
    // 修改表单是否提交过状态
    param.$submitted = true;

    // 表单校验
    if (param.$invalid) {
      return false;
    }

    // // 添加操作人员
    this.workerIds = [];
    this.workerNames = [];

    $.each(this.teamWithOrgList,function(index,value){
      $.each(value.teamList,function(index1,team){
        $.each(team.workerInfoBeanList,function(index2,worker){
          if (worker.checked) {
            _this.workerIds.push(worker.id);
            _this.workerNames.push(worker.workerName);
          }
        });
      });
    });

    this.editableEquipmentInfo.operator = this.workerNames.join(',');
    this.editableEquipmentInfo.operatorId = this.workerIds.join(',');

    // 把文件上传返回token传入this.editableEquipmentInfo
    this.editableEquipmentInfo.image = this.avatarUploadToken.id;
    // 获得上传附件的id列表 附件
    this.editableEquipmentInfo.accessory = this.editableEquipmentInfo.accessoryFileBeans.map((item) =>{
      return item.id;
    }).join(',');

    $http.post(`${SERVER_CONTEXT}/api/deviceinfo/insertDeviceInfo`,
      this.editableEquipmentInfo).
      then((resp) => {
        if (resp.data.resultCode == 0){
          layer.msg(resp.data.resultMsg);
          $state.go('equipmentInfoDetail', {id: resp.data.id});
        } else {
          layer.alert(resp.data.resultMsg);
        }
      });
  }

  // 重置设备新增
  this.reset = () => {
    this.editableEquipmentInfo.$reset();
    this.editableEquipmentInfo.orgId = $rootScope.activeBid.orgId;
  }

  this.getWorkersByTeamId = (team) => {

    if (team.initFlag) {
      team.isShowWorker = !!!team.isShowWorker;
      return;
    }
    $http
      .get(`${SERVER_CONTEXT}/api/education/queryEducationWorkerByCheck/${team.teamId}/${param.id}`)
      .then((resp) => {
        team.initFlag = true;
        team.workerInfoBeanList = resp.data;
        team.isShowWorker = !!!team.isShowWorker;
        team.workerInfoBeanList.forEach((worker) => {
          worker.checked = worker.checked ? true : false;
          worker.constructionTeamName = team.teamName;
          worker.constructionTeamId = team.teamId;
        });
        // 判断是否全选
        team.checked = true;
        team.workerInfoBeanList.forEach((worker) => {
          if (!worker.checked){
            team.checked = false;
          }
        });
      });
  }

  // 获得施工队list
  paFetchService
    .getTeamWithOrgList()
    .then((resp) => {
      this.teamWithOrgList = resp.data;
    });


}

export default equipmentInfoNewCtrl;